/**
 * Copyright (C), 2015-2018, XXX有限公司
 * FileName: QuartzConfigration
 * Author:   周宏硕
 * Date:     2018/12/19 14:09
 * Description:
 * History:
 * <author>          <time>          <version>          <desc>
 * 作者姓名           修改时间           版本号              描述
 */
package com.ht.temp.configurations.config;

import com.ht.temp.configurations.component.JobFactory;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;

/**
 * 〈一句话功能简述〉<br> 
 * 〈〉
 *
 * @author 周宏硕
 * @create 2018/12/19
 * @since 1.0.0
 */
@Configuration
public class QuartzConfigration  implements ApplicationListener<ContextRefreshedEvent> {
   @Autowired
   private JobFactory jobFactory;



    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {
        try {
            jobFactory.scheduleJobs();
        } catch (SchedulerException e) {
            e.printStackTrace();
        }

    }

    @Bean
    public SchedulerFactoryBean schedulerFactoryBean() {
        SchedulerFactoryBean schedulerFactoryBean = new SchedulerFactoryBean();
        schedulerFactoryBean.setJobFactory(jobFactory);
        // 用于quartz集群,QuartzScheduler 启动时更新己存在的Job
        schedulerFactoryBean.setOverwriteExistingJobs(true);
        schedulerFactoryBean.setStartupDelay(1);
        return schedulerFactoryBean;
    }

    @Bean
    public Scheduler scheduler() {
        return schedulerFactoryBean().getScheduler();
    }

}
